﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace BlimpControlPanel
{
    public partial class MotorControl : UserControl
    {
        /// <summary>
        /// This is the initializer for the motor control handler.  This handles the buttons necessary
        /// for a vehicle to operate.  This figures out based on the vehicle type selected, the command
        /// that needs to be sent to the vehicle.
        /// </summary>
        public MotorControl()
        {
            InitializeComponent();

            cbControlType.SelectedItem = Properties.Settings.Default.VehicleType;
        }

        /// <summary>
        /// Sets the command status label to the success or failure values.
        /// </summary>
        /// <param name="success">
        /// True if the command was a success and false if a
        /// failure occurred.
        /// </param>
        public void SetCommandStatusLabel(bool success)
        {
            if (success)
            {
                lblCmdStatus.BackColor = Color.Green;
                lblCmdStatus.Text      = "Successful";
            }
            else
            {
                lblCmdStatus.BackColor = Color.Red;
                lblCmdStatus.Text      = "Failed";
            }
        }

        /// <summary>
        /// This function is used to reset all the status labels for the system.  This is used
        /// for both the initialize and disconnect/connect functions.
        /// </summary>
        public void ResetCommandStatusLabel()
        {
            lblCmdStatus.BackColor = btnGoDown.BackColor;
            lblCmdStatus.ForeColor = Color.Black;
            lblCmdStatus.Text      = "Command Status";
        }

        private void btnSendMotorCommands_Click(object sender, EventArgs e)
        {
            if (sender.GetType() == typeof(System.Windows.Forms.Button))
               SendCommand(((System.Windows.Forms.Button)sender).Name);
        }

       private void SendCommand(string senderName)
       {
          string outString = "";

          switch (senderName)
          {
             case "btnMotorsOn":
                if (cbControlType.Text == "Blimp")
                    outString = "ON";
                else
                   outString = "FORWARD";
                break;
             case "btnMotorsOff":
                outString = "OFF";
                break;
             case "btnTurnLeft":
                outString = "LEFT";
                break;
             case "btnTurnRight":
                outString = "RIGHT";
                break;
             case "btnGotoWaypoint":
                outString = "WAYPOINT";
                break;
             case "btnGotoHome":
                outString = "HOME";
                break;
             case "btnGoDown":
                if (cbControlType.Text == "Blimp")
                   outString = "DOWN";
                else
                   outString = "BACK";
                break;
             case "btnGoUp":
                if (cbControlType.Text == "Blimp")
                    outString = "UP";
                else
                    outString = "ON";
                break;
             case "cbLightsOn":
                Debug.WriteLine("LIGHTS");
                if (cbControlType.Text == "Ground Vehicle")
                   outString = "LIGHTS";
                break;
             default:
                return;
          }

          if (writeCommand != null)
             writeCommand(outString);
       }

        /// <summary>
        /// This is the delegate definition for the message handler.
        /// </summary>
        /// <param name="message">Message that is to be sent to the bus.</param>
        public delegate void WriteCommandHandler(string message);

        /// <summary>
        /// This is the function that should be called when a command is to be written
        /// to the bus.
        /// </summary>
        public event WriteCommandHandler writeCommand;

        private void cbControlType_SelectedIndexChanged(object sender, EventArgs e)
        {
           if (cbControlType.Text == "Blimp")
              SwitchControlsToBlimp();
           else if (cbControlType.Text == "Ground Vehicle")
              SwitchControlsToGroundVehicle();
        }

        private void SwitchControlsToGroundVehicle()
        {
            cbLightsOn.Visible = true;
            cbLightsOn.Checked = false;
        }

        private void SwitchControlsToBlimp()
        {
            cbLightsOn.Visible = false;
         }

        private void cbLightsOn_CheckedChanged(object sender, EventArgs e)
        {
            SendCommand(((CheckBox)sender).Name);

            if (!cbLightsOn.Checked)
                cbLightsOn.ImageIndex = 0;
            else
                cbLightsOn.ImageIndex = 1;
        }
    }
}
